package dataAccessLayer.factoryMySQL.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dataAccessLayer.abstractFactory.daos.DAOsalesDetail;
import dataAccessLayer.dataBase.DBManager;
import dataAccessLayer.visualObject.VOsaleDetail;

public class DAOsalesDetailMySQL implements DAOsalesDetail
{
	private DBManager dbManager;
	
	public DAOsalesDetailMySQL(DBManager dbManager) 
	{
		this.dbManager = dbManager;
	}

	public boolean insertSaleDetail(VOsaleDetail vOsaleDetail) 
	{
		PreparedStatement preparedStatement = null;
		boolean success = true;

		Connection connection = dbManager.getConnection();

		try
		{
			preparedStatement = connection.prepareStatement("INSERT INTO salesDetail (pie,sale,quantity) VALUES (?,?,?)");

			preparedStatement.setInt(1, vOsaleDetail.getPie());
			preparedStatement.setInt(2, vOsaleDetail.getSale());
			preparedStatement.setInt(3, vOsaleDetail.getQuantity());

			preparedStatement.execute();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			success = false;
		}
		finally
		{
			try
			{
				preparedStatement.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return success;
	}

	public boolean removeSaleDetail(VOsaleDetail vOsaleDetail) 
	{
		PreparedStatement preparedStatement = null;
		boolean success = true;
		Connection connection = dbManager.getConnection();
		
		try
		{
			preparedStatement = connection.prepareStatement("DELETE FROM salesDetail WHERE pie=? AND sale=?");
			
			preparedStatement.setInt( 1 , vOsaleDetail.getPie());
			preparedStatement.setInt( 2 , vOsaleDetail.getSale());
			
			preparedStatement.execute();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			success = false;
		}
		finally
		{
			try
			{
				preparedStatement.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return success;
	}
	
	public ArrayList<VOsaleDetail> listSalesDetail(VOsaleDetail vOsaleDetail) 
	{
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<VOsaleDetail> result = null;

		Connection connection = dbManager.getConnection();
		try
		{
			preparedStatement = connection.prepareStatement( "SELECT pie,sale,quantity FROM salesDatail WHERE sale=?" );
			preparedStatement.setInt(1, vOsaleDetail.getSale());
			
			resultSet = preparedStatement.executeQuery();

			result = new ArrayList<VOsaleDetail>();

			while( resultSet.next() )
			{
				VOsaleDetail saleDetail = new VOsaleDetail();
				saleDetail.setPie( resultSet.getInt(1) );
				saleDetail.setSale( resultSet.getInt(2) );
				saleDetail.setQuantity(resultSet.getInt(3));

				result.add( saleDetail);
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				resultSet.close();
				preparedStatement.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return result;
	}
	
}
